package org.indi.graduatemanagement.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.indi.graduatemanagement.common.lang.Result;
import org.indi.graduatemanagement.entity.Graduates;
import org.indi.graduatemanagement.service.GraduatesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zcl
 * @since 2021-04-24
 */
@RestController
@RequiresAuthentication //登录后可查询
@RequestMapping("/graduates")
public class GraduatesController {

    @Autowired
    GraduatesService graduatesService;


    @GetMapping("/{id}")
    @RequiresPermissions("search")
    @RequiresAuthentication //登录后可查询
    public Result getGraduatesInfoById(@PathVariable("id") String id) {
        Graduates graduates = graduatesService.getById(id);
        Assert.notNull(graduates, "学号不存在");
        return Result.successful(graduates);
    }

    @GetMapping("/all")
    @RequiresPermissions("search")
    @RequiresAuthentication //登录后可查询
    public Result getAllGraduatesInfo() {
        List<Graduates> graduatesList = graduatesService.list();
        return Result.successful(graduatesList);
    }

    @GetMapping("/list")
    @RequiresAuthentication //登录后可查询
    @RequiresPermissions("search")
    public Result getGraduatesInfoByCollegeAndMajor(@RequestParam(value = "college") String college, @RequestParam(value = "major", required = false) String major) {
        List<Graduates> graduatesList = graduatesService.list(new QueryWrapper<Graduates>().eq("graduates_college", college));
        if (CollectionUtils.isEmpty(graduatesList)) {
            return Result.fail("未找到该学院或该学院无毕业生");
        }
        if (major != null) {
            graduatesList.removeIf(graduates -> !graduates.getGraduatesMajor().equals(major));
        }
        return Result.successful(graduatesList);
    }

    @PostMapping("/update")
    @RequiresPermissions("update")
    @RequiresAuthentication //登录后可查询
    public Result updateGraduatesInfo(@Validated @RequestBody Graduates graduates) {
        if (graduatesService.getById(graduates.getGraduatesId()) == null) {
            return Result.fail("该毕业生信息不存在");
        }
        graduatesService.updateById(graduates);
        return Result.successful(null);
    }

    @PostMapping("/save")
    @RequiresPermissions("add")
    @RequiresAuthentication //登录后可查询
    public Result saveGraduatesInfo(@Validated @RequestBody Graduates graduates) {
        if (graduatesService.getById(graduates.getGraduatesId()) != null) {
            return Result.fail("毕业生信息已存在");
        }
        graduatesService.save(graduates);
        return Result.successful(null);
    }

    @RequiresPermissions("delete")
    @DeleteMapping("/{id}")
    @RequiresAuthentication //登录后可查询
    public Result deleteGraduatesInfoById(@PathVariable("id") String id) {
        if (graduatesService.removeById(id))
            return Result.successful(null);
        else
            return Result.fail("删除失败");
    }
}
